<?php
// +----------------------------------------------------------------------
// |首页控制器
// +----------------------------------------------------------------------
// |Data:2016-06-27 下午7:02
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Controller;
use Common\Controller\CommonController;

class IndexController extends CommonController{
    private $_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Common/Shop');
    }

    public function index(){
        $type = I('get.type',0);//1评论最多 2距离最近 3销售量 4起送价
        $map = $this->search();

        $ord = 'add_time desc';
        if($type == 3){
            $ord = 'sales_nums desc';
        }else if($type == 4){
            $ord = 'min_send_price desc';
        }else if($type == 1){
            $ord = 'comment_num desc';
        }
        //$arr = array('8497.32','8110.74');
        $list = $this->_model->getHomeShopList($map,$ord);
        foreach($list as $key=>&$val){
            list($s_time,$e_time) = explode(',',$val['sales_time']);
            $val['s_time'] = $s_time;
            $val['e_time'] = $e_time;
            $devilerArr = unserialize($val['deviler_price']);
            asort($devilerArr['sprice']);
            $val['deviler_price'] = $devilerArr['sprice'][0];
            if($val['free_price']){
                $freeArr = unserialize($val['free_price']);
                asort($freeArr['dprice']);
                $val['free_price'] = $freeArr['dprice'][0];
            }else{
                $val['free_price'] = 0;
            }
            $val['pic'] = showImage($val['pic'],'shop',$val['uid'])[0];
            $val['score'] = D('Wap/Comment')->getShopScore($val['id']);
            list($lng,$lat) = explode(',',$val['location']);
            $val['distance'] = getDistance($this->location,$lat.','.$lng);
            //$val['distance'] = $arr[$key];
        }

        if($type == 2){
            $list =  $this->distance($list);
        }

        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->display();
    }

    /**
     * 距离最近
     * @param $data
     * @return bool
     */
    private function distance($data){
        if(empty($data)) return false;
        foreach($data as $key=>$val){
            $distance[$key] = $val['distance'];
        }
        array_multisort($distance, SORT_ASC, $data);
        return $data;
    }

    private function search(){
        $cid = I('get.cid',0);
        $status = I('get.status',0);//1营业中 2可预定
        $map = array();
        $map['id'] = array('gt',0);
        !empty($cid) && $map['cate_id'] = $cid;
        if($status == 1){
            $map['status'] = 1;
        }else if($status == 2){
            $map['is_reservation'] = 1;
        }

        $cateList = D('Admin/ShopCate')->getList();
        $this->assign('cateList',$cateList);
        $this->assign('cid',$cid);
        $this->assign('status',$status);
        return $map;
    }

    /**
     * 店铺介绍
     * @return bool
     */
    public function detail(){
        $id = I('get.id');
        if(empty($id)){
            redirect('Index/index');
        }
        session('shop_id',$id);
        $detail = $this->_model->detail($id);
        $detail['pic'] = showImage($detail['pic'],'shop',$detail['uid']);
        $detail['shopCate'] = D('Admin/ShopCate')->getField($detail['cate_id']);
        list($s_time,$e_time) = explode(',',$detail['sales_time']);
        $detail['s_time'] = $s_time;
        $detail['e_time'] = $e_time;
        $detail['devilerArr'] = unserialize($detail['deviler_price']);
        !empty($detail['free_price']) && $detail['freeArr'] = unserialize($detail['free_price']);
        list($d_stime,$d_etime) = explode(',',$detail['deviler_time']);
        $detail['d_stime'] = $d_stime;
        $detail['d_etime'] = $d_etime;
        $area = D('Common/Area')->getFullNameByAreaCode($detail['area_code']).$detail['address'];

        $couponsList = D('Common/Coupons')->getShopCouponsList($id);
        foreach($couponsList as &$val){
            $isHas = D('Common/Coupons')->hasUseRecord($this->uid,$val['id']);
            $val['isRecord'] = $isHas ? 1 : 2;
        }
        $detail['distance'] = $detail['devilerArr']['smax_km'][count($detail['devilerArr']['smax_km']) - 1];
        $map = array();
        $map['shop_id'] = $id;
        $map['status'] = 1;
        $noticeList = D('Admin/Notice')->getAll($map);
        $commentList = D('Wap/Comment')->getTopCommentList($id,3);
        foreach($commentList as &$val){
            $user = D('Wap/Info')->getUserInfo($val['uid']);
            $val['pic'] = showImage($user['face'],'face',$val['uid'])[0];
        }
        $isCollection = D('Wap/Collection')->isHas($id,$this->uid);
        $this->assign('noticeList',$noticeList);
        $this->assign('commentList',$commentList);
        $this->assign('couponsList',$couponsList);
        $this->assign('detail',$detail);
        $this->assign('isCollection',$isCollection ? 1 : 2);
        list($lng,$lat) = explode(',',$detail['location']);
        $this->assign('area',$area);
        $this->assign('lng',$lng);
        $this->assign('lat',$lat);
        $this->display();
    }

    /**
     * 评论列表
     */
    public function comment(){
        $shop_id = I('get.id');
        if(empty($shop_id)) return false;
        $commentDetail = $this->getShopScore($shop_id);
        $shopDetail = D('Common/Shop')->detail($shop_id);
        $list = D('Wap/Comment')->getList(array('shop_id'=>$shop_id),'add_time desc,replay_time desc');
        $score = D('Wap/Comment')->getShopScore($shop_id);
        $this->assign('commentTotal',$shopDetail['comment_num']);
        $this->assign('commentDetail',$commentDetail);
        $this->assign('score',$score);
        $this->assign($list);
        $this->display();
    }

    /**
     * 获取店铺评论人数
     * @param $shop_id
     * @return array
     */
    private function getShopScore($shop_id){
        $allList = D('Wap/Comment')->getScoreNumList($shop_id);
        $data = array();
        foreach($allList as $val){
            switch($val['score']){
                case 1:
                    $data['one'][] = $val['score'];
                    break;
                case 2:
                    $data['two'][] = $val['score'];
                    break;
                case 3:
                    $data['three'][] = $val['score'];
                    break;
                case 4:
                    $data['four'][] = $val['score'];
                    break;
                case 5:
                    $data['five'][] = $val['score'];
                    break;
            }
        }
        return $data;
    }

    /**
     * 领取优惠券
     */
    public function receive(){
        $id = I('post.id');
        empty($id) && $this->returnData('请选择操作项!');

        $isHas = D('Common/Coupons')->hasRecord($id,$this->uid);
        !empty($isHas) && $this->returnData('您已领取了!');

        $detail = D('Common/Coupons')->detail($id);
        if($detail['store'] < 1) $this->returnData('该优惠券已领取完!');

        $data = array(
            'coupons_id'=>$id,
            'uid'=>$this->uid,
            'receive_time'=>time(),
            'end_time'=>$detail['ext_time'],
            'is_use'=>2,
            'is_overdue'=>2
        );
        $res = D('Common/Coupons')->addRecord($data);
        if($res){
            $this->returnData('领取成功!',1);
        }else{
            $this->returnData('领取失败!');
        }
    }

    /**
     * 收藏
     */
    public function collection(){
        $shop_id = I('post.id');
        empty($shop_id) && $this->returnData('请选择操作项!');

        $isHas = D('Wap/Collection')->isHas($shop_id,$this->uid);
        $type = $isHas ? 2 : 1;
        $res = D('Wap/Collection')->doCollection($shop_id,$this->uid,$type);
        if($res){
            $this->returnData('',$type);
        }else{
            $this->returnData('操作失败!');
        }
    }

    /**
     * 评论列表页面
     */
    public function comment_list(){
        $shop_id = I('get.id');
        if(empty($shop_id)) return false;
        $detail = $this->_model->detail($shop_id);
        $score = D('Wap/Comment')->getShopScore($shop_id);
        $commentList = D('Wap/Comment')->getTopCommentList($shop_id,10);
        $scoreData = $this->getShopScore($shop_id);
        $allList = D('Wap/Comment')->getScoreNumList($shop_id);
        $scoreNum=array();
        $scoreOneNum=$scoreTwoNum=$scoreThreeNum=$scoreFourNum=$scoreFiveNum=0;
        foreach($allList as $val){
            switch($val['score']){
                case 1:
                    $scoreOneNum++;
                    $scoreNum['one']= $scoreOneNum;
                    break;
                case 2:
                    $scoreTwoNum++;
                    $scoreNum['two']= $scoreTwoNum;
                    break;
                case 3:
                    $scoreThreeNum++;
                    $scoreNum['three']= $scoreThreeNum;
                    break;
                case 4:
                    $scoreFourNum++;
                    $scoreNum['four']= $scoreFourNum;
                    break;
                case 5:
                    $scoreFiveNum++;
                    $scoreNum['five']= $scoreFiveNum;
                    break;
            }
        }
        $commentTotal = count($scoreNum);
        $this->assign('detail',$detail);
        $this->assign('score',$score);
        $this->assign('commentList',$commentList);
        $this->assign('commentTotal',$commentTotal);
        $this->assign('scoreData',$scoreData);
        $this->assign('scoreNum',$scoreNum);
        $this->display('comment_list');
    }
}
?>